﻿using WebSelfHost.Entity.Sys;
using WebSelfHost.Logic.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using WebSelfHost.Utility.Core;
using WebSelfHost.Utility.Operator;

namespace WebSelfHost.Logic.Sys
{
    /// <summary>
    /// 字典管理表数据访问
    /// </summary>
    public class SysItemLogic : BaseLogic
    {
        /// <summary>
        /// 获得所有的字典信息
        /// </summary>
        /// <returns></returns>
        public List<SysItem> GetList()
        {
            using (var db = GetInstance())
            {
                return db.Queryable<SysItem>().Where(it => it.DeleteMark == "0").OrderBy(it => it.SortCode).ToList();
            }
        }

        /// <summary>
        /// 获得所有的字典信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<SysItem> GetAppList(int pageIndex, int pageSize, ref int totalCount)
        {
            using (var db = GetInstance())
            {
                return db.Queryable<SysItem>().Where(it => it.DeleteMark == "0" && it.ParentId != "0").OrderBy(it => it.SortCode).ToPageList(pageIndex, pageSize, ref totalCount);
            }
        }

        /// <summary>
        /// 获得所有的字典信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="keyWord"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<SysItem> GetList(int pageIndex, int pageSize, string keyWord, ref int totalCount)
        {
            using (var db = GetInstance())
            {
                if (keyWord.IsNullOrEmpty())
                {
                    return db.Queryable<SysItem>().Where(it => it.DeleteMark == "0").OrderBy(it => it.SortCode).ToPageList(pageIndex, pageSize, ref totalCount);
                }
                return db.Queryable<SysItem>().Where(it => it.DeleteMark == "0" && (it.Name.Contains(keyWord) || it.EnCode.Contains(keyWord))).OrderBy(it => it.SortCode).ToPageList(pageIndex, pageSize, ref totalCount);
            }
        }


        /// <summary>
        /// 根据父节点得到所有子节点
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public int GetChildCount(string parentId)
        {
            using (var db = GetInstance())
            {
                return db.Queryable<SysItem>().Where(it => it.ParentId == parentId).ToList().Count();
            }
        }

        /// <summary>
        /// 根据Id得到字典信息
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public SysItem Get(string primaryKey)
        {
            using (var db = GetInstance())
            {
                return db.Queryable<SysItem>().Where(it => it.Id == primaryKey).First();
            }
        }

        /// <summary>
        /// 新增字典
        /// </summary>
        /// <param name="model"></param>
        /// <param name="account"></param>
        /// <returns></returns>
        public int AppInsert(SysItem model, string account)
        {
            using (var db = GetInstance())
            {
                SysItem s = db.Queryable<SysItem>().Where(it => it.ParentId == "0").First();
                model.Id = Guid.NewGuid().ToString().Replace("-", "");
                model.Layer = s.Layer + 1;
                model.ParentId = s.Id;
                model.IsEnabled = "1";
                model.DeleteMark = "0";
                model.CreateUser= account;
                model.CreateTime = DateTime.Now;
                model.ModifyUser = model.CreateUser;
                model.ModifyTime = model.CreateTime;
                return db.Insertable<SysItem>(model).ExecuteCommand();
            }
        }

        /// <summary>
        /// 新增字典
        /// </summary>
        /// <param name="model"></param>
        /// <param name="account"></param>
        /// <returns></returns>
        public int Insert(SysItem model, string account)
        {
            using (var db = GetInstance())
            {
                model.Id = Guid.NewGuid().ToString().Replace("-", "");
                model.Layer = Get(model.ParentId).Layer += 1;
                model.IsEnabled = model.IsEnabled == null ? "0" : "1";
                model.DeleteMark = "0";
                model.CreateUser =account;
                model.CreateTime = DateTime.Now;
                model.ModifyUser = model.CreateUser;
                model.ModifyTime = model.CreateTime;
                return db.Insertable<SysItem>(model).ExecuteCommand();
            }
        }


        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public int Delete(string primaryKey)
        {
            using (var db = GetInstance())
            {
                return db.Deleteable<SysItem>().Where(it => it.Id == primaryKey).ExecuteCommand();
            }
        }
        /// <summary>
        /// 更新字典信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="account"></param>
        /// <returns></returns>
        public int Update(SysItem model, string account)
        {
            using (var db = GetInstance())
            {
                model.Layer = Get(model.ParentId).Layer += 1;
                model.IsEnabled = model.IsEnabled == null ? "0" : "1";
                model.ModifyUser = account;
                model.ModifyTime = DateTime.Now;
                return db.Updateable<SysItem>(model).UpdateColumns(it => new
                {
                    it.ParentId,
                    it.Layer,
                    it.EnCode,
                    it.Name,
                    it.SortCode,
                    it.IsEnabled,
                    it.Remark,
                    it.ModifyUser,
                    it.ModifyTime
                }).ExecuteCommand();
            }
        }

        /// <summary>
        /// 更新字典信息
        /// </summary>
        /// <param name="model"></param>
        /// <param name="operateUser"></param>
        /// <returns></returns>
        public int AppUpdate(SysItem model, string operateUser)
        {
            using (var db = GetInstance())
            {
                model.ModifyUser = operateUser;
                model.ModifyTime = DateTime.Now;
                return db.Updateable<SysItem>(model).UpdateColumns(it => new
                {
                    it.EnCode,
                    it.Name,
                    it.SortCode,
                    it.Remark,
                    it.ModifyUser,
                    it.ModifyTime
                }).ExecuteCommand();
            }
        }
    }
}
